package com.blueocean_health.care.management.controller;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.AccountListDto;
import com.blueocean_health.care.management.domain.dto.UpdateNurseDto;
import com.blueocean_health.care.management.em.AccountFlagEnum;
import com.blueocean_health.care.management.em.OrderTypeEnum;
import com.blueocean_health.care.management.exception.OrderServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.ordervo.OrderInfoVo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.OrderInfoService;

/**
 * @Author ChenPeng cp
 * @Date 2017/8/21 10:14
 */

@RestController
@RequestMapping(value = {"/web/order","test/order/new"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderInfoController{

    @Autowired
    private OrderInfoService orderInfoService;


    @RequestMapping("/add")
    public SysResult insertOrder(@RequestBody OrderInfoVo orderInfoVo) throws OrderException {
    	User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        Integer userid = courrentUser.getUserId();
        String hid =  courrentUser.getHospitalId();
        Long version = orderInfoVo.getVersion();
        SysResult sysResult = orderInfoService.insertOrderInfo(orderInfoVo,hid,userid,version, OrderTypeEnum._1.getKey());
        return sysResult;
    }


    @RequestMapping("/refund")
    public SysResult refundOrder(@RequestBody OrderInfoVo orderInfoVo) throws OrderException {
        User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        Integer userid = courrentUser.getUserId();
        String hid =  courrentUser.getHospitalId();
        Long version = orderInfoVo.getVersion();
        SysResult sysResult = orderInfoService.insertOrderInfo(orderInfoVo,hid,userid,version, OrderTypeEnum._3.getKey());
        return sysResult;
    }


    @RequestMapping("/status/modify")
    public SysResult orderModify(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String orderStatus = (String) jsonObject.get("orderStatus");
        String id = (String) jsonObject.get("id");
        Integer oid = Integer.parseInt(id);

        String[] status = {"1","6"};
        int flag = Arrays.binarySearch(status,orderStatus);
        if (flag < 0){
            return SysResult.error("params is fail");
        }
        return orderInfoService.updateOrderInfoStatus(orderStatus,oid);
    }

    @RequestMapping("/delete")
    public SysResult deleteOrderInfo(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String orderId = (String) jsonObject.get("orderId");
        return orderInfoService.deleteOrderInfo("1",orderId);
    }

    /**
    * @Description: 编辑正常订单时，提示会批量删除退款单时调用
    * @param:入参 正常订单的orderid
    * @return:出参
    * @throws: OrderException
    */
    @RequestMapping("/refund/delete")
    public SysResult deleteRefundOrderList(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String orderId = (String) jsonObject.get("orderId");
        orderInfoService.deleteRefundOrderAndUpdateServiceRefundFlag(orderId);
        return SysResult.ok();
    }

    /**
    * @Description: 订单的入账和取消入账
    * @param:入参
    * @return:出参
    * @throws:
    */
    @RequestMapping("/account/modify")
    public SysResult orderAccount(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String accountFlag = (String) jsonObject.get("accountFlag");
        String time = (String) jsonObject.get("time");
        Integer oid = (Integer) jsonObject.get("id");

        String status = "1,0";
        boolean flag = status.contains(accountFlag);
        if (!flag){
            return SysResult.error("params is fail");
        }
        return orderInfoService.updateOrderAccount(accountFlag,oid,time);
    }

    /**
     * @Description: 订单的批量入账
     * @param:入参
     * @return:出参
     * @throws:
     */
    @RequestMapping("/account/modifyList")
    public SysResult orderAccountList(@RequestBody AccountListDto dto) throws OrderException {
        int total = dto.getId().length;
        int success = 0;
        int fail = 0;
        String accountTime = dto.getTime();
        String msg = null;
        if (dto.getId() == null || dto.getId().length < 1){
            msg = "请勾选需要入账的订单！";
            return  SysResult.error(msg);
        }

        for (Integer id: dto.getId()) {
            SysResult sysResult = orderInfoService.updateOrderAccount(AccountFlagEnum._0.getKey(),id,accountTime);
            if (sysResult.getCode().equals(0)){
                success = success + 1;
            }
        }
        fail = total - success;

       if (fail > 0){
           msg = success+"个订单成功入账，"+fail+"个失败";
           return  SysResult.error().addMsg(msg);
       }
       return SysResult.ok();
    }

    /**
     * @Description:单项服务下拉框
     * @param:入参
     * @return:出参
     * @throws:
     */
    @RequestMapping("/getSingleService")
    public SysResult getSingleService() throws OrderException {
        User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid =  courrentUser.getHospitalId();
        return orderInfoService.getSingleService(0);
    }


    /**
     * @Description: 更换护工
     * @param:入参
     * @return:出参
     * @throws:
     */
    @RequestMapping("/updateNurse")
    public SysResult updateNurse(@RequestBody UpdateNurseDto dto) throws OrderServiceException {
        User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid =  courrentUser.getHospitalId();
        dto.setHid(hid);
        SysResult sysResult = orderInfoService.updateNurse(dto);
        return sysResult;
    }



}
